// main.c
#include <stdio.h>
#include <stdlib.h>
#include "../include/link_pub.h"

void display_menu() {
    printf("========= 学生选课系统 =========\n");
    printf("1. 学生信息管理\n");
    printf("2. 课程信息管理\n");
    printf("3. 学生选课\n");
    printf("4. 日志信息\n");
    printf("0. 退出\n");
    printf("=================================\n");
    printf("请输入操作编号: ");
}

void student_management_menu() {
    printf("=========学生信息管理菜单:========\n");
    printf("1. 新增学生\n");
    printf("2. 删除学生\n");
    printf("3. 修改学生信息\n");
    printf("4. 查看学生信息\n");
    printf("0. 返回上一级菜单\n");
    printf("=================================\n");
    printf("请输入操作编号: ");
}

void course_management_menu() {
    printf("=========课程信息管理菜单:========\n");
    printf("1. 新增课程\n");
    printf("2. 删除课程\n");
    printf("3. 修改课程信息\n");
    printf("4. 查看课程信息\n");
    printf("0. 返回上一级菜单\n");
    printf("=================================\n");
    printf("请输入操作编号: ");
}

void log_management_menu() {
    printf("========日志信息管理菜单:=========\n");
    printf("1. 分析日志\n");
    printf("2. 搜索学生日志\n");
    printf("3. 搜索课程日志\n");
    printf("0. 返回上一级菜单\n");
    printf("=================================\n");
    printf("请输入操作编号: ");
}

int main() {
    int choice, sub_choice;
    char student_id[20], student_name[50], course_id[20], course_name[50];
    char course_nature[20];
    int hours, credits, max_students;

    // 加载初始数据
    load_students();
    load_courses();

    while (1) {
        display_menu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                while (1) {
                    student_management_menu();
                    scanf("%d", &sub_choice);
                    if (sub_choice == 0) {
                        break; // 返回上一级菜单
                    }
                    switch (sub_choice) {
                        case 1:
                            printf("请输入学生ID: ");
                            scanf("%s", student_id);
                            printf("请输入学生姓名: ");
                            scanf("%s", student_name);
                            add_student(student_id, student_name);
                            printf("学生已新增\n");
                            break;
                        case 2:
                            printf("请输入要删除的学生ID: ");
                            scanf("%s", student_id);
                            delete_student(student_id);
                            printf("学生已删除\n");
                            break;
                        case 3:
                            printf("请输入学生ID: ");
                            scanf("%s", student_id);
                            printf("请输入新的学生姓名: ");
                            scanf("%s", student_name);
                            update_student(student_id, student_name);
                            printf("学生信息已更新\n");
                            break;
                        case 4:
                            list_students();
                            printf("请输入学生ID: ");
                            scanf("%s", student_id);
                            view_student(student_id);
                            break;
                        case 0:
                            return 0;
                        default:
                            printf("无效的输入，请重新选择。\n");
                    }
                }
                break;

            case 2:
                while (1) {
                    course_management_menu();
                    scanf("%d", &sub_choice);
                    if (sub_choice == 0) {
                        break; // 返回上一级菜单
                    }
                    switch (sub_choice) {
                        case 1:
                            printf("请输入课程ID: ");
                            scanf("%s", course_id);
                            printf("请输入课程名称: ");
                            scanf("%s", course_name);
                            printf("请输入课程性质 (必修/选修): ");
                            scanf("%s", course_nature);
                            printf("请输入授课学时: ");
                            scanf("%d", &hours);
                            printf("请输入学分: ");
                            scanf("%d", &credits);
                            printf("请输入课程人数上限: ");
                            scanf("%d", &max_students);
                            add_course(course_id, course_name, course_nature, hours, credits, max_students);
                            printf("课程已新增\n");
                            break;
                        case 2:
                            printf("请输入要删除的课程ID: ");
                            scanf("%s", course_id);
                            delete_course(course_id);
                            printf("课程已删除\n");
                            break;
                        case 3:
                            printf("请输入课程ID: ");
                            scanf("%s", course_id);
                            printf("请输入新的授课学时: ");
                            scanf("%d", &hours);
                            printf("请输入新的学分: ");
                            scanf("%d", &credits);
                            printf("请输入新的课程人数上限: ");
                            scanf("%d", &max_students);
                            update_course(course_id, hours, credits, max_students);
                            printf("课程信息已更新\n");
                            break;
                        case 4:
                            list_courses();
                            printf("请输入课程ID: ");
                            scanf("%s", course_id);
                            view_course(course_id);
                            break;
                        case 0:
                            return 0;
                        default:
                            printf("无效的输入，请重新选择。\n");
                    }
                }
                break;

            case 3:
                // 学生选课菜单
                printf("=========学生选课菜单:============\n");
                printf("1. 学生选课\n");
                printf("0. 返回上一级菜单\n");
                printf("=================================\n");
                printf("请输入操作编号: ");
                scanf("%d", &sub_choice);
                if (sub_choice == 1) {
                    printf("请输入学生ID: ");
                    scanf("%s", student_id);
                    printf("请输入要选的课程ID: ");
                    scanf("%s", course_id);
                    select_course(student_id, course_id);
                } else if (sub_choice == 0) {
                    continue;
                } else {
                    printf("无效的输入，请重新选择。\n");
                }
                break;

            case 4:
                while (1) {
                    log_management_menu();
                    scanf("%d", &sub_choice);
                    if (sub_choice == 0) {
                        break; // 返回上一级菜单
                    }
                    switch (sub_choice) {
                        case 1:
                            analyze_log();
                            break;
                        case 2:
                            printf("请输入要查找的学生ID: ");
                            scanf("%s", student_id);
                            search_student_log(student_id);
                            break;
                        case 3:
                            printf("请输入要查找的课程ID: ");
                            scanf("%s", course_id);
                            search_course_log(course_id);
                            break;
                        case 0:
                            return 0;
                        default:
                            printf("无效的输入，请重新选择。\n");
                    }
                }
                break;

            case 0:
                printf("退出系统...\n");
                exit(0);
                break;

            default:
                printf("无效的输入，请重新选择。\n");
        }

        printf("\n"); // 每次操作后换行以便更好阅读
    }

    return 0;
}
